package com.lzy.cloud.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.cloud.base.dto.BaseCategoryDto;
import com.lzy.cloud.base.entity.BaseCategory;
import com.lzy.cloud.base.mapper.BaseCategoryMapperExt;
import com.lzy.cloud.base.service.CategoryService;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private BaseCategoryMapperExt baseCategoryMapperExt;

    @Override
    public Long insert(BaseCategoryDto baseCategoryDto) {
        BaseCategory baseCategory = DozerUtils.map(baseCategoryDto, BaseCategory.class);
        return baseCategoryMapperExt.insertSelective(baseCategory) > 0 ? baseCategory.getId() : 0;
    }

    @Override
    public int update(BaseCategoryDto baseCategoryDto) {
        return baseCategoryMapperExt.updateByPrimaryKeySelective(DozerUtils.map(baseCategoryDto, BaseCategory.class));
    }

    @Override
    public BaseCategoryDto findById(Long aLong) {
        return DozerUtils.map(baseCategoryMapperExt.selectByPrimaryKey(aLong), BaseCategoryDto.class);
    }

    @Override
    public PageInfo<BaseCategoryDto> getList(Pager pager) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        return new PageInfo<>(baseCategoryMapperExt.getList());
    }

    @Override
    public int deleteById(Long id) {
        return baseCategoryMapperExt.deleteById(id);
    }
}
